﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace MyPluginApplication
{
    public partial class formMain : Form
    {
        public delegate void MyFinishDelegate();
        public event MyFinishDelegate OnFinish;

        public formMain()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("conf.xml");
                XmlNode language = doc.SelectSingleNode("/MyPluginApplication/configuration");
                if (language.InnerText == "English")
                {
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en");
                    InitializeComponent();
                    englishToolStripMenuItem.Checked = true;
                }
                else if (language.InnerText == "Czech")
                {
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("cs");
                    InitializeComponent();
                    czechToolStripMenuItem.Checked = true;
                }
                else
                {
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;
                    InitializeComponent();
                    this.defaultToolStripMenuItem.Checked = true;
                }

            }
            catch (Exception e)
            {
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;
                InitializeComponent();
                this.defaultToolStripMenuItem.Checked = true;
            }

            Debug.WriteLine("\n" + DateTime.Now + " DEBUG: Aplikace běží v {0} s  AppDomainID = {1}.", Thread.GetDomain().FriendlyName, Thread.GetDomainID());
            OnFinish += new MyFinishDelegate(Form1_OnFinish);
        }

        void Form1_OnFinish()
        {
            Debug.Fail("Tato debug.fail hlaska je vyvolana vlastni udalosti pri zavreni aplikace");
            Trace.WriteLine(DateTime.Now + " UKONCENI APLIKACE - Tato debug.fail hlaska je vyvolana vlastni udalosti pri zavreni aplikace");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close(); //User clicked File > Exit
        }

        private void formMain_Load(object sender, EventArgs e)
        {
            //Call the find plugins routine, to search in our Plugins Folder
            Global.Plugins.FindPlugins(Application.StartupPath + @"\Plugins");

            //Add each plugin to the treeview
            foreach (Types.AvailablePlugin pluginOn in Global.Plugins.AvailablePlugins)
            {
                TreeNode newNode = new TreeNode(pluginOn.Instance.Name);
                this.tvwPlugins.Nodes.Add(newNode);
            }

        }

        private void tvwPlugins_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //Make sure there's a selected Plugin
            if (this.tvwPlugins.SelectedNode != null)
            {
                //Get the selected Plugin\
                Types.AvailablePlugin selectedPlugin = Global.Plugins.AvailablePlugins.Find(tvwPlugins.SelectedNode.Text);

                if (selectedPlugin != null)
                {
                    this.lblPluginName.Text = selectedPlugin.Instance.Name;
                    this.lblPluginVersion.Text = "(" + selectedPlugin.Instance.Version + ")";
                    this.lblPluginAuthor.Text = "By: " + selectedPlugin.Instance.Author;
                    this.lblPluginDesc.Text = selectedPlugin.Instance.Description;

                    this.pnlPlugins.Controls.Clear();

                    this.pnlPlugins.Controls.Add(selectedPlugin.Instance.MainInterface);
                }

            }
        }

        private void czechToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("cs");
            setLanguge("Czech");
        }

        private void setLanguge(string language)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlNode root = doc.CreateElement("MyPluginApplication");
                XmlNode config = doc.CreateElement("configuration");
                XmlNode lang = doc.CreateElement("Language");
                lang.InnerText = language;
                // samotny koren vlozime do dokumentu
                config.AppendChild(lang);
                root.AppendChild(config);
                doc.AppendChild(root);
                // ulozime dokument
                doc.Save("conf.xml");
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
            }
            Application.Restart();
        }

        private void englishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en");
            setLanguge("English");
        }

        private void defaultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;
            setLanguge("Default");
        }

        protected override void OnClosing(CancelEventArgs e)
        {

            if (OnFinish != null)
            {
                this.OnFinish();
            }

            base.OnClosing(e);
        }
    }
}
